﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBuy.DataAccessLayer;
using System.Drawing;
using ZedGraph;

namespace IBuy.Entity
{

    abstract class Chart
    {
        protected List<Color> defaultColors = new List<Color>();

        #region 私有变量
        private string title;
        private string xItemName;
        private string yItemName;
        private ZedGraphControl graph;
        private string itemName;
        #endregion
        #region 公共属性
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        public string XItemName
        {
            get { return xItemName; }
            set { xItemName = value; }
        }
        public string YItemName
        {
            get { return yItemName; }
            set { yItemName = value; }
        }
        public ZedGraphControl Graph
        {
            get { return graph; }
            set { graph = value; }
        }
        public string ItemName
        {
            get { return itemName; }
            set { itemName = value; }
        }
        #endregion
        public abstract void Draw();

        protected void InitDefaultColors()
        {
            defaultColors.Add(Color.Red);
            defaultColors.Add(Color.Green);
            defaultColors.Add(Color.Blue);
            defaultColors.Add(Color.Yellow);
            defaultColors.Add(Color.YellowGreen);
            defaultColors.Add(Color.Brown);
            defaultColors.Add(Color.Aqua);
            defaultColors.Add(Color.SaddleBrown);
            defaultColors.Add(Color.DarkSeaGreen);
            defaultColors.Add(Color.Indigo);
            defaultColors.Add(Color.SlateBlue);
            defaultColors.Add(Color.SeaShell);
        }
    }

    class Histogram : Chart
    {
        
        public override void Draw()
        {
            GraphPane myPane = new GraphPane() ;

            myPane.Title.Text = Title;
            myPane.XAxis.Title.Text = XItemName;
            myPane.YAxis.Title.Text = YItemName;

            ChartHelp help = new ChartHelp(ItemName);
            help.GetNamesAndValues();
            string[] labels = help.Names;
            double[] y = help.Values;
            // Generate a red bar with "Curve 1" in the legend
            BarItem myBar = myPane.AddBar("数量", null, y, Color.Red);
            myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);

            // Draw the X tics between the labels instead of 
            // at the labels
            myPane.XAxis.MajorTic.IsBetweenLabels = true;

            // Set the XAxis labels
            myPane.XAxis.Scale.TextLabels = labels;
            // Set the XAxis to Text type
            myPane.XAxis.Type = AxisType.Text;

            // Fill the Axis and Pane backgrounds
            myPane.Chart.Fill = new Fill(Color.White,
                  Color.FromArgb(255, 255, 166), 90F);
            myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));

            Graph.GraphPane = myPane;
            // Tell ZedGraph to refigure the
            // axes since the data have changed
            Graph.AxisChange();
        }
    }

    class LineChart : Chart
    {
        public override void Draw()
        {
            throw new NotImplementedException();
        }

    }

    class PieChart : Chart
    {
        public override void Draw()
        {
            InitDefaultColors();
            GraphPane myPane = new GraphPane();

            myPane.Title.Text = Title;
            myPane.XAxis.Title.Text = XItemName;
            myPane.YAxis.Title.Text = YItemName;

            ChartHelp help = new ChartHelp(ItemName);
            help.GetNamesAndValues();
            string[] labels = help.Names;
            double[] y = help.Values;

            for (int i = 0; i < y.GetLength(0); i++)
            {
                myPane.AddPieSlice(y[i], defaultColors[i], Color.White, 45f, 0.01, labels[i]);
            }

            // Draw the X tics between the labels instead of 
            // at the labels
            myPane.XAxis.MajorTic.IsBetweenLabels = true;

            // Set the XAxis labels
            myPane.XAxis.Scale.TextLabels = labels;
            // Set the XAxis to Text type
            myPane.XAxis.Type = AxisType.Text;

            // Fill the Axis and Pane backgrounds
            myPane.Chart.Fill = new Fill(Color.White,
                  Color.FromArgb(255, 255, 166), 90F);
            myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
            Graph.GraphPane = myPane;
            // Tell ZedGraph to refigure the
            // axes since the data have changed
            Graph.AxisChange();
        }
    }


}
